package com.xy.biz.xd.service.impl;

import com.xy.biz.mc.client.MessagePushClient;
import com.xy.biz.mc.domain.Message;
import com.xy.biz.mc.domain.consts.MessageTypeConstant;
import com.xy.biz.mc.domain.req.MessagePushRequest;
import com.xy.biz.uc.client.UserClient;
import com.xy.biz.xd.domain.bo.*;
import com.xy.biz.xd.domain.converter.DocumentFlowBorrowAndPrintAndDropConverter;
import com.xy.biz.xd.domain.req.ApplyDocumentFlowPrintRequest;
import com.xy.biz.xd.domain.req.ReleaseDocumentFlowNewRequest;
import com.xy.biz.xd.manager.*;
import com.xy.biz.xd.service.DocumentFlowPrintService;
import com.xy.lang.domain.Result;
import com.xy.lang.domain.tuples.Triple;
import com.xy.lang.util.ResultUtils;
import com.xy.tool.context.UserContextHolder;
import com.xy.tool.gid.service.GidService;
import com.xy.tool.json.JsonUtils;
import com.xy.tool.util.BeanUtilsEx;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.xy.biz.fd.domain.DocumentPermissions.*;
import static com.xy.biz.xd.domain.bo.DocumentFlowConstants.*;
import static com.xy.biz.xd.domain.bo.DocumentReleaseBO.*;
import static java.util.stream.Collectors.toList;

@Service
public class DocumentFlowPrintServiceImpl extends AbstractDocumentFlowBorrowAndPrintAndDropServiceImpl implements DocumentFlowPrintService {

    @Autowired
    private DocumentFlowServiceSupport documentFlowServiceSupport;
    @Autowired
    DocumentFlowBorrowAndPrintAndDropManager documentFlowBorrowAndPrintAndDropManager;
    @Autowired
    private DocumentFlowManager documentFlowManager;
    @Autowired
    private DocumentFlowHandleManager documentFlowHandleManager;
    @Autowired
    private DocumentReleaseManager documentReleaseManager;
    @Autowired
    private MessagePushClient messagePushClient;
    @Autowired
    private UserClient userClient;

    private String type = "下载审批";

    @Override
    protected String getType() {
        return type;
    }

    @Transactional
    @Override
    public Result<?> saveDocumentFlow(ApplyDocumentFlowPrintRequest request) {
        List<DocumentFlowBorrowAndPrintAndDropBO> documentFlowBorrowAndPrintAndDropBOList = DocumentFlowBorrowAndPrintAndDropConverter.toBOList(request);
        DocumentFlowBorrowAndPrintAndDropBO documentFlowPrintBO = buildDocumentFlowBorrowAndPrintAndDropList(documentFlowBorrowAndPrintAndDropBOList, FLOW_TYPE_DOWNLOAD);
        Task task = documentFlowServiceSupport.startFirstTask(documentFlowPrintBO, PROCESS_COMMON_FLOW, request.getNextHandleUserId());
        String foreignKey = task.getProcessInstanceId();
        documentFlowPrintBO.setForeignKey(foreignKey);
        int row = documentFlowBorrowAndPrintAndDropManager.saveAllDocumentFlowBorrowAndPrintAndDrop(documentFlowBorrowAndPrintAndDropBOList);
        if (row > 0) {
            //定义消息内容
            Message build = Message.builder()
                    .type(MessageTypeConstant.TYPE_FLOW)
                    .data(1)
                    .path(MessageTypeConstant.FLOW_LIST)
                    .remark("下载审批流程-文件审核")
                    .build();
            //发送消息
            messagePushClient.send(MessagePushRequest.builder()
                    .data(JsonUtils.toJson(build).getBytes())
                    .dataType(0)
                    .expiredSeconds(0L)
                    .toId(request.getNextHandleUserId())
                    .offlineFlag(true).build());
        }
        return Result.success();
    }

    @Override
    public Result<?> releaseDocumentFlow(ReleaseDocumentFlowNewRequest request) {
        DocumentFlowBO documentFlowBO = documentFlowManager.getDocumentFlow(request.getDocumentFlowId());
        if (Objects.isNull(documentFlowBO)) {
            return Result.ofNotFound(String.valueOf(request.getDocumentId()));
        }
        Integer batchType = documentFlowBO.getBatchType();
        Result<Triple<List<DocumentFlowBO>, List<Long>, List<Long>>> result;
        if (!(result = saveDocumentReleaseList(request, documentFlowBO, batchType)).wasSuccess()) {
            return result;
        }
        Triple<List<DocumentFlowBO>, List<Long>, List<Long>> triple = ResultUtils.get(result);
        //定义消息内容
        Message message = Message.builder()
                .type(MessageTypeConstant.TYPE_FLOW)
                .data(0)
                .remark("下载审批流程有一条新的放行文件-去签收")
                .path(MessageTypeConstant.DOCUMENT_RELEASE_CHECK)
                .build();
        //发送消息
        Set<Long> userIdSet = buildMessageReceiveUserIdCollection(triple.getValue2(), triple.getValue3());
        for (Long userId : userIdSet) {
            sendMessage(message, userId);
        }
        return Result.success();
    }
}
